//=============================================================================
//     _   _                               ____                _  _          
//    | | | |  __ _  _ __   _ __   _   _  / ___|  _ __   _ __ (_)| |_   ___ 
//    | |_| | / _` || '_ \ | '_ \ | | | | \___ \ | '_ \ | '__|| || __| / _ \
//    |  _  || (_| || |_) || |_) || |_| |  ___) || |_) || |   | || |_ |  __/
//    |_| |_| \__,_|| .__/ | .__/  \__, | |____/ | .__/ |_|   |_| \__| \___|
//                  |_|    |_|     |___/         |_|                         
//
//                     HappySprite - We make sprites happy
//
// Copyright (c) 2007 by Tank Monkey Games
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//=============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace HappySprite.Audio
{
    public class AudioManager : GameComponent, IDisposable
    {
        AudioEngine engine;
        WaveBank waveBank;
        SoundBank soundBank;

        public AudioManager(Game game)
            : base(game)
        {
            game.Components.Add(this);
        }

        public void LoadAudioEngine(string settingsFile)
        {
            // Check for existing engine
            CheckEngineIsNull();

            engine = new AudioEngine(settingsFile);
        }

        public void LoadSoundBank(string filename)
        {
            // Check that engine exists
            CheckEngineExists();

            // Dispose of existing
            if (soundBank != null) soundBank.Dispose();

            soundBank = new SoundBank(engine, filename);
        }

        public void LoadWaveBank(string filename)
        {
            // Check that engine exists
            CheckEngineExists();

            // Dispose of existing
            if (waveBank != null) waveBank.Dispose();

            waveBank = new WaveBank(engine, filename);
        }

        public Cue Play(string name)
        {
            // Check that soundBank exists
            CheckSoundBankExists();

            Cue cue = soundBank.GetCue(name);
            cue.Play();
            return cue;
        }

        public void PlayToEnd(string name)
        {
            // Check that soundBank exists
            CheckSoundBankExists();

            soundBank.PlayCue(name);
        }

        public void Stop(Cue cue)
        {
            cue.Stop(AudioStopOptions.Immediate);
        }

        public override void Update(GameTime gameTime)
        {
            if (engine != null)
                engine.Update();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (soundBank != null) soundBank.Dispose();
                if (waveBank != null) waveBank.Dispose();
                if (engine != null) engine.Dispose();
            }

            base.Dispose(disposing);
        }

        void CheckEngineIsNull()
        {
            if (engine != null) throw new InvalidOperationException("An audio engine has all ready been created.");
        }

        void CheckEngineExists()
        {
            if (engine == null) throw new InvalidOperationException("An audio engine must be created first. Use LoadAudioEngine() to create an audio engine.");
        }

        void CheckSoundBankExists()
        {
            if (soundBank == null) throw new InvalidOperationException("A sound bank must be created first. Use LoadSoundBank() to create a sound bank.");
        }
    }
}
